package jgl.vajra.management.part.mine.activity;

import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.LieEarnAmountBean;
import jgl.vajra.management.bean.LieEarnCommissionBean;
import jgl.vajra.management.constant.Constant;
import jgl.vajra.management.databinding.ActivityLieEarnBinding;
import jgl.vajra.management.dialogFragment.ShareFriendsDialogFragment;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.mine.contract.LieEarnContract;
import jgl.vajra.management.part.mine.viewModel.LieEarnViewModel;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.giide.GlideImageLoader;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(LieEarnViewModel.class)
public class LieEarnActivity extends BaseMVVMActivity<LieEarnViewModel, ActivityLieEarnBinding> implements LieEarnContract.View {

    private SingleTypeBindingAdapter adapter;
    private ShareFriendsDialogFragment shareFriendsDialogFragment;
    private String balance;

    @Override
    public int getLayoutId() {
        return R.layout.activity_lie_earn;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        String avatar = getIntent().getStringExtra("avatar");
        String nickname = getIntent().getStringExtra("nickname");

        GlideImageLoader.onAvatarDisplayRound(mBinding.ivAvatar, avatar, null);
        mBinding.tvName.setText(nickname);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_lie_earn);
        mBinding.recyclerView.setAdapter(adapter);

        requestNetData();
    }

    public void requestNetData() {

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);

        //累计多少钱
        mViewModel.getLieEarnData(map);

        //余额
        mViewModel.getLieEarnBalanceData(map);

        Map<String, Object> mapLevel = new HashMap<>();
        mapLevel.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mapLevel.put("userType", Constant.USER_TYPE);
        //推销员等级
        mViewModel.getSpreadLevelData(mapLevel);

        //佣金标准
        mViewModel.getLieEarnCommissionData(mapLevel);
    }

    //二维码
    @Override
    public void onBusinessCode() {
//        IntentManager.toBusinessCodeActivity(this);

        if (shareFriendsDialogFragment == null)
            shareFriendsDialogFragment = new ShareFriendsDialogFragment();
        shareFriendsDialogFragment.setShareData(this);
        shareFriendsDialogFragment.show(this.getSupportFragmentManager());
    }

    //提现
    @Override
    public void onWithdrawal() {
        //IntentManager.toWithdrawalPageActivity(this,balance);
        if (TextUtils.isEmpty(balance)){
            return;
        }

        int newBalance = Integer.parseInt(balance);
        if (newBalance==0) {
            ToastUtils.showShort("提现金额不能小于等于0");
            return;
        }

        Map<String,Object> map=new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("amount", AEACCoder.encrypt(balance));
        map.put("tag",1);
        mViewModel.getWithdrawalBalanceData(map);
    }

    //佣金明细
    @Override
    public void onCommissionDetails() {
        IntentManager.toCommissionDetailsActivity(this);
    }

    //提现记录
    @Override
    public void onWithdrawalRecord() {
        IntentManager.toWithdrawalRecordActivity(this);
    }

    //普通推广员
    @Override
    public void onGeneralPromoter() {
        ToastUtils.showShort("普通推广员");
    }

    //我的团队
    @Override
    public void onMyTeam() {
        IntentManager.toMyTeamActivity(this);
    }

    @Override
    public void returnLieEarnAmountData(LieEarnAmountBean data) {
        mBinding.tvContractor.setText("包工头团队帮你赚银子：累计" + data.clientCount + "两");
        mBinding.tvLocomotive.setText("机车佬团队帮你赚银子：累计" + data.ownerCount + "两");
        mBinding.tvMaster.setText("大师傅团队帮你赚银子：累计" + data.driverCount + "两");

        int totalCount = data.clientCount + data.ownerCount + data.driverCount;
        mBinding.tvTotalCount.setText("你和下属团队帮你赚银子：累计" + totalCount + "两");

        mBinding.tvCurrentRanking.setText(data.grade+"名");
        mBinding.tvCurrentTime.setText("团队赚银排名用时"+data.useDay+"天");
        mBinding.tvFirst.setText("目前排在首位的赚钱佣金合计："+data.firstRebate+"两银，上线时间"+data.firstUseDay+"天。");
    }

    @Override
    public void returnLieEarnCommissionBean(List<LieEarnCommissionBean> data) {
        adapter.refresh(data);
    }

    @Override
    public void returnSpreadLevelData(String data) {
        mBinding.tvSpreadLevel.setText(data);
    }

    @Override
    public void returnLieEarnBalanceData(String data) {
        String  newBalance = AEACCoder.decrypt(data);
        //截取&前面的约
        balance = newBalance.substring(0, newBalance.indexOf("&"));
        mBinding.tvBalance.setText(balance);
    }
}
